package kz.edu.sdu.atm.server;

import java.util.Map;

import kz.edu.sdu.atm.db.IGenericDbAdapter;
import kz.edu.sdu.atm.models.OperationType;
import kz.edu.sdu.atm.models.Request;
import kz.edu.sdu.atm.models.Response;
import kz.edu.sdu.atm.models.ResponseStatus;
import kz.edu.sdu.atm.models.UserDTO;

public abstract class IServer {
	public static String STORE_FILE_NAME = "store.txt";

	IGenericDbAdapter storeAdapter;

	public abstract void init();

	public abstract void destroy();

	public IGenericDbAdapter getAdapter() {
		return storeAdapter;
	}
	public abstract void auth(UserDTO user, Map<String, String> args,
			Response res);

	public abstract void balance(UserDTO user, Map<String, String> args,
			Response res);

	public abstract void withdraw(UserDTO user, Map<String, String> args,
			Response res);

	public abstract void send(UserDTO user, Map<String, String> args,
			Response res);

	public abstract void deposit(UserDTO user, Map<String, String> args,
			Response res);

	public void request(Request req, Response res) {
		UserDTO user = storeAdapter.getUser(req.getUsername());
		if (user == null) {
			res.set(ResponseStatus.AUTH, "No such login or password");
			return;
		} else if (!user.getPassword().equals(req.getPassword())) {
			res.set(ResponseStatus.AUTH, "No such login or password");
			return;
		}
		if (req.getOperation().equals(OperationType.AUTH)) {
			auth(user, req.getMap(), res);
			return;
		}
		if (req.getOperation().equals(OperationType.BALANCE)) {
			balance(user, req.getMap(), res);
			return;
		}
		if (req.getOperation().equals(OperationType.DEPOSIT)) {
			deposit(user, req.getMap(), res);
			return;
		}
		if (req.getOperation().equals(OperationType.SEND)) {
			send(user, req.getMap(), res);
			return;
		}
		if (req.getOperation().equals(OperationType.WITHDRAW)) {
			withdraw(user, req.getMap(), res);
			return;
		}
	}
}
